package org.linlinjava.litemall.admin.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.dto.AdminScheduledJobLogDTO;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallScheduledJobLog;
import org.linlinjava.litemall.db.service.LitemallScheduledJobLogService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AdminScheduledJobLogService {

    private final LitemallScheduledJobLogService scheduledJobLogService;

    public ResponseUtil.Response logs(AdminScheduledJobLogDTO scheduledJobLogDTO) {
        IPage<LitemallScheduledJobLog> scheduledJobLogs = scheduledJobLogService.page(new Page<>(scheduledJobLogDTO.getPage(), scheduledJobLogDTO.getLimit()),Wrappers.lambdaQuery(LitemallScheduledJobLog.class)
                .isNotNull(LitemallScheduledJobLog::getId)
                .and(wrapper -> wrapper
                        .isNotNull(LitemallScheduledJobLog::getId)
                        .like(StrUtil.isNotBlank(scheduledJobLogDTO.getDesc()), LitemallScheduledJobLog::getAddTime, scheduledJobLogDTO.getDesc()).or()
                        .isNotNull(LitemallScheduledJobLog::getId)
                        .like(StrUtil.isNotBlank(scheduledJobLogDTO.getDesc()), LitemallScheduledJobLog::getId, scheduledJobLogDTO.getDesc()).or()
                        .isNotNull(LitemallScheduledJobLog::getId)
                        .like(StrUtil.isNotBlank(scheduledJobLogDTO.getDesc()), LitemallScheduledJobLog::getClassName, scheduledJobLogDTO.getDesc()).or()
                        .isNotNull(LitemallScheduledJobLog::getId)
                        .like(StrUtil.isNotBlank(scheduledJobLogDTO.getDesc()), LitemallScheduledJobLog::getJobName, scheduledJobLogDTO.getDesc()).or()
                        .isNotNull(LitemallScheduledJobLog::getId)
                        .like(StrUtil.isNotBlank(scheduledJobLogDTO.getDesc()), LitemallScheduledJobLog::getJobGroup, scheduledJobLogDTO.getDesc()).or()
                        .isNotNull(LitemallScheduledJobLog::getId)
                        .like(StrUtil.isNotBlank(scheduledJobLogDTO.getDesc()), LitemallScheduledJobLog::getCron, scheduledJobLogDTO.getDesc()).or()
                        .isNotNull(LitemallScheduledJobLog::getId)
                        .like(StrUtil.isNotBlank(scheduledJobLogDTO.getDesc()), LitemallScheduledJobLog::getDesc, scheduledJobLogDTO.getDesc()).or()
                        .isNotNull(LitemallScheduledJobLog::getId)
                        .like(StrUtil.isNotBlank(scheduledJobLogDTO.getDesc()), LitemallScheduledJobLog::getParameter, scheduledJobLogDTO.getDesc()).or()
                        .isNotNull(LitemallScheduledJobLog::getId)
                        .like(StrUtil.isNotBlank(scheduledJobLogDTO.getDesc()), LitemallScheduledJobLog::getTaskName, scheduledJobLogDTO.getDesc())
                )
                .orderByDesc(LitemallScheduledJobLog::getAddTime));
        return ResponseUtil.okPage(scheduledJobLogs);
    }
}
